﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports System.Drawing
Imports System.Drawing.Imaging

Public Class Schriftengine

    ''' <summary>
    ''' Eine Schrift Art
    ''' </summary>
    ''' <remarks></remarks>
    Structure Font
        Public Sub New(ByVal Groesse As Integer, ByVal neuSchriftart As Schriftarten, ByVal Stil As System.Drawing.FontStyle)
            Me.Groesse = Groesse
            Me.Schriftart = neuSchriftart.ToString
            Me.Stil = Stil
            LoadFont()
        End Sub
        Dim Schrift As Direct3D.Font
        Public Function GetFont() As Direct3D.Font
            Return Schrift
        End Function
        Dim Schriftart As String
        Public Function GetSchriftart() As String
            Return Schriftart
        End Function
        Dim Groesse As Integer
        Public Function GetGroesse() As Integer
            Return Groesse
        End Function
        Dim Stil As System.Drawing.FontStyle

        Public Sub UnloadFont()
            Schrift.Dispose()
        End Sub
        Public Sub LoadFont()
            Dim systemfont As System.Drawing.Font
            systemfont = New System.Drawing.Font(Schriftart.ToString.Replace("_", " "), Groesse, Stil, GraphicsUnit.Pixel)
            Schrift = New Direct3D.Font(Grafikengine.Grafikkarte, systemfont)
            systemfont.Dispose()
        End Sub
        Public Sub OnLostDevice()
            Schrift.OnLostDevice()
        End Sub
        Public Sub OnResetDevice()
            Schrift.OnResetDevice()
        End Sub
    End Structure


    Public Enum Schriftarten
        Arial
        Century
        Elephant
        Comic_Sans_MS
        Courier_New
        Arial_Unicode_MS
        Comic_Sans_MS_Bold
    End Enum
    Public Shared Texte() As ObjektText = {}
    Public Shared Fonts As New List(Of Font)
    Public Shared MeasureSpriteLocker As New Mutex
    ''' <summary>
    ''' Dieser Sprite ist ausschliesslich zur Schriftgroessen bestimmung gedacht.
    ''' Verwende ihn nie zum Malen.
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared MeasureSprite As Sprite
    Public Sub New()
    End Sub
    Public Shared Function Malen() As Boolean
        Grafikengine.Sprite.Transform = Matrix.Identity
        If Texte.Length > 0 Then
            For I = 0 To Texte.Length - 1
                Texte(I).Malen(New Point)
            Next
        End If
    End Function
    Public Shared Sub Schriftloeschen(ByVal Text As Integer)
        Texte(Text) = Texte(Texte.Length - 1)
        Array.Resize(Texte, Texte.Length - 1)
    End Sub
    Public Shared Sub Schrifterstellen(ByVal neuText() As String, ByVal X As Integer, ByVal Y As Integer, ByVal neueFarbe() As Color, ByVal neueSchriftart As Schriftengine.Schriftarten, ByVal neuSchriftgroesse As Integer, ByVal Stil As System.Drawing.FontStyle, Optional ByVal neuSchreiben As Boolean = False)
        Array.Resize(Texte, Texte.Length + 1)
        Texte(Texte.Length - 1) = New ObjektText(neuText, X, Y, neueFarbe, neueSchriftart, neuSchriftgroesse, Stil, neuSchreiben)
    End Sub
    Public Shared Function CreateSchriftart(ByVal neueSchriftart As Schriftarten, ByVal neueSchriftgroesse As Integer, ByVal Stil As System.Drawing.FontStyle) As Integer
        Dim NeuTextart As String = neueSchriftart.ToString
        For I = 0 To Fonts.Count - 1
            If Fonts(I).Schriftart = NeuTextart Then
                If Fonts(I).Groesse = neueSchriftgroesse Then
                    If Fonts(I).Stil = Stil Then
                        Return I
                    End If
                End If
            End If
        Next
        'Font Objekt erzeugen

        Fonts.Add(New Font(neueSchriftgroesse, neueSchriftart, Stil))
        Return Fonts.Count - 1
    End Function
    Public Shared Sub DrawText(ByVal Text As ObjektText, ByVal Pos As Point)
        Text.Malen(Pos)
    End Sub
    ''' <summary>
    ''' Malt den Text auf den Bildschirm
    ''' </summary>
    ''' <param name="Zeile"></param>
    ''' <param name="Schriftart"></param>
    ''' <param name="Farbe"></param>
    ''' <remarks></remarks>
    Public Shared Sub DrawTextZeile(ByVal Zeile As String, ByVal Position As Point, ByVal Schriftart As Integer, ByVal Farbe As Color)
        'Damit nicht gezoomt wird.
        Grafikengine.Sprite.Transform = Matrix.Identity
        Fonts(Schriftart).Schrift.DrawText(Grafikengine.Sprite, Zeile, Position, Farbe)
    End Sub
    ''' <summary>
    ''' Malt den Text auf den Bildschirm
    ''' </summary>
    ''' <param name="Zeilen"></param>
    ''' <param name="Schriftart"></param>
    ''' <param name="Farbe"></param>
    ''' <remarks></remarks>
    Public Shared Sub DrawTextZeile(ByVal Zeilen As List(Of String), ByVal Position As Point, ByVal Schriftart As Integer, ByVal Farbe As Color)
        Grafikengine.Sprite.Transform = Matrix.Identity
        'Damit nicht gezoomt wird.
        Dim H As Integer = Fonts(Schriftart).Schrift.MeasureString(Grafikengine.Sprite, "H", DrawTextFormat.None, Color.Black).Height
        For I = 0 To Zeilen.Count - 1
            Fonts(Schriftart).Schrift.DrawText(Grafikengine.Sprite, Zeilen(I), Position, Farbe)
            Position.Y += H
        Next
    End Sub

End Class
